import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from "@angular/common/http";

import { Observable, of } from 'rxjs';
import { MessageService } from 'src/app/message.service';
import { ItemData } from './ItemData';
import { OutputDesignDtoData } from './OutputDesignDtoData';

@Injectable({
  providedIn: 'root',
})
export class OutputDesignDtoService {

  constructor(private http:HttpClient, private messageService: MessageService) { }

  getScreenIdList(): Observable<Array<string>> {
    // TODO: send the message _after_ fetching the heroes
    // this.messageService.add('SrcTypeService: fetched SrcTypes');

    return this.http.get<Array<string>>("getScreenIdList");
  }

  getDesignScreenDtoList(screenId : string, filterValue : string): Observable<ItemData[]> {
    // TODO: send the message _after_ fetching the heroes
    // this.messageService.add('SrcTypeService: fetched SrcTypes');

    const data = {
      'screenId': screenId,
      'filterValue':filterValue
   };

    return this.http.get<ItemData[]>("getDesignScreenDtoList", {params: data});
  }

  register(outputDesignDtoData: OutputDesignDtoData,screenId : string, filterValue : string): Observable<any> {

    const data = {
      'screenId': screenId,
      'filterValue':filterValue
   };
    //const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' })};
    const outputDesignDtoDataJson = JSON.stringify(outputDesignDtoData);
    return this.http.post('registerDto', outputDesignDtoDataJson,
      { headers: new HttpHeaders({ 'Content-Type': 'application/json' }), responseType: 'blob', observe: 'response', params:data});
  }

  // register(outputDesignDtoData : OutputDesignDtoData): Observable<any> {
  //   // TODO: send the message _after_ fetching the heroes
  //   // this.messageService.add('SrcTypeService: fetched SrcTypes');
  //   const httpOptions = {headers: new HttpHeaders({'Content-Type': 'application/json'})};
  
  //   const outputDesignDtoDataP = JSON.stringify(outputDesignDtoData);

  //   return this.http.post("register", outputDesignDtoDataP, httpOptions);
  // }

  // register(outputDesignDtoData : OutputDesignDtoData): Observable<any> {
  //   // TODO: send the message _after_ fetching the heroes
  //   // this.messageService.add('SrcTypeService: fetched SrcTypes');
  //   // const httpOptions = {headers: new HttpHeaders({'Content-Type': 'application/json'})};
  //   const httpOptions = {headers: new HttpHeaders({'Content-Type': 'application/json','responseType': 'blob'})};
  
  //   const outputDesignDtoDataP = JSON.stringify(outputDesignDtoData);

  //   return this.http.post("register", outputDesignDtoDataP, httpOptions);
  // }

  // exportExcel() {
  //   const httpOptions = new HttpHeaders().set('Content-Type', 'application/json');

  //   this.http.get<any>("exportExcel", {httpOptions, responseType: 'blob'}).subscribe(
  //     // data => {
  //     //   if(this.listOfData.length === 0){
  //     //     return;
  //     //   }
  //     // }
  //     // (base64Pdf: string) => {
  //     //   const arrayBuffer = base64ToArrayBuffer(base64Pdf); // 创建Array缓冲区
  //     //   createAndDownloadBlobFile(arrayBuffer, 'testName');
  //     // },
  //     // error => console.error(error)
      
  //     // data => thefile = new Blob([data], { type: "application/octet-stream" }), //console.log(data),
  //     //               error => console.log("Error downloading the file."),
  //     //               () => console.log('Completed file download.')
  //     response => {

  //           let blob = new Blob([response], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
  //           saveAs(blob, 'test.xlsx');
  //         });
  // }

}
